package com.wudl.java8;

import com.alibaba.fastjson.JSON;
import com.wudl.java8.bean.JsonoBbject;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.*;

import static com.alibaba.fastjson.JSONPatch.OperationType.remove;
import static org.apache.commons.lang3.StringUtils.isEmpty;

/**
 * @ClassName : ObjectIsNull
 * @Description : 判断对象的属性是否为空
 * @Author :wudl
 * @Date: 2021-08-26 23:01
 */

public class ObjectIsNull {

    public static void main(String[] args) {


        String json = "{\"date\":\"20181020\",\"stories\":[{\"title\":\"每周一吸 · 狸花猫\",\"ga_prefix\":\"102013\",\"images\":[\"a\"],\"multipic\":true,\"type\":0,\"id\":9698943},{\"images\":[\"bbg\"],\"type\":0,\"id\":9698988,\"ga_prefix\":\"102012\",\"title\":\"大误 · 救救马云\"},]}";

        JsonoBbject jsonoBbject = JSON.parseObject(json, JsonoBbject.class);
        System.out.println(JSON.toJSONString(jsonoBbject));
        jsonoBbject.getStories().get(1).setGa_prefix(" ");
        String s = JSON.toJSONString(jsonoBbject);
        System.out.println("json---"+s);
        Map map = JSON.parseObject(s, Map.class);


        boolean b = removeNullEntry(map);
        System.out.println(b);

    }




    /**

     * 移除map中空key或者value空值

     * @param map

     */

    public static boolean removeNullEntry(Map map){
        removeNullKey(map);

        boolean b = removeNullValue(map);
        System.out.println(b);
        return b;

    }

    /**

     * 移除map的空key

     * @param map

     * @return

     */

    public static boolean removeNullKey(Map map){
        Set set = map.keySet();

        for (Iterator iterator = set.iterator(); iterator.hasNext();) {
            Object obj = (Object) iterator.next();

            boolean remove1 = remove(obj, iterator);

        }
        return true;

    }

    /**

     * 移除map中的value空值

     * @param map

     * @return

     */

    public static boolean removeNullValue(Map map) {
        Set set = map.keySet();

        for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
            Object obj = (Object) iterator.next();

            Object value = (Object) map.get(obj);

            boolean removeBoolean = remove(value, iterator);
            return removeBoolean;

        }
        return true;
    }

        private static boolean remove(Object obj,Iterator iterator){
            if(obj instanceof String){
                String str = (String)obj;

                if(isEmpty(str)){

                   return false;

                }

            }else if(obj instanceof Collection){
                Collection col = (Collection)obj;

                if(col==null||col.isEmpty()){
                    return false;

                }

            }else if(obj instanceof Map){
                Map temp = (Map)obj;

                if(temp==null||temp.isEmpty()){
                    return false;
                }

            }else if(obj instanceof Object[]){
                Object[] array =(Object[])obj;

                if(array==null||array.length<=0){
                    return false;
                }

            }else{
                if(obj==null){
                    return false;
                }

            }
    return true;
        }

        public static boolean isEmpty(Object obj){
            return obj == null || obj.toString().length() == 0;

        }

}
